﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

using COUP.Core.Enums;

namespace COUP.Core.Files
{
    public static class MagicType
    {
        public static Dictionary<uint, MagicTypeEntry> Parse()
        {
            Dictionary<uint, MagicTypeEntry> MagicData = new Dictionary<uint, MagicTypeEntry>();

            while (!Monitor.TryEnter(Console.Out, 50)) { }
            Console.Write("[Game Server] Loading Magic Data...");
            int Left = Console.CursorLeft;
            FileStream File = new FileStream(Settings.MagicType, FileMode.Open);
            BinaryReader Reader = new BinaryReader(File);

            int start = System.Environment.TickCount;
            uint Amount = Reader.ReadUInt32();

            MagicTypeEntry _MDetail;

            File.Seek(4 * Amount, SeekOrigin.Current);

            for (int i = 0; i < Amount; i++)
            {
                _MDetail = new MagicTypeEntry();
                _MDetail.SpellID = Reader.ReadUInt32();
                _MDetail.Method = (MagicAttackMethod)Reader.ReadUInt32();

                for (int x = 0; x < 16; x++)
                {
                    _MDetail.SpellName += (char)Reader.ReadByte();
                }
                _MDetail.SpellName = _MDetail.SpellName.Trim('\0');


                Console.Write(_MDetail.SpellID);
                Console.CursorLeft = Left;

                _MDetail.Aggressive = Convert.ToBoolean(Reader.ReadUInt32());

                File.Seek(16, SeekOrigin.Current);

                _MDetail.ManaReq = (ushort)Reader.ReadUInt32();
                _MDetail.StatusEffect = Reader.ReadUInt32();
                _MDetail.SuccessRate = (byte)Reader.ReadUInt32();
                _MDetail.Timer = Reader.ReadUInt32();

                File.Seek(16, SeekOrigin.Current);

                uint Unknown4 = Reader.ReadUInt32();
                _MDetail.ExpReq = Reader.ReadUInt32();

                File.Seek(48, SeekOrigin.Current);

                _MDetail.UpgradeSkillID = Reader.ReadUInt32();

                File.Seek(4, SeekOrigin.Current);

                uint Unknown6 = Reader.ReadUInt32();
                uint Unknown7 = Reader.ReadUInt32();

                for (int x = 0; x < 64; x++)
                {
                    _MDetail.Upgrade += (char)Reader.ReadByte();
                }
                _MDetail.Upgrade = _MDetail.Upgrade.Trim('\0');

                uint Unknown8 = Reader.ReadUInt32();
                uint Unknown9 = Reader.ReadUInt32();
                uint Unknown10 = Reader.ReadUInt32();

                File.Seek(244, SeekOrigin.Current);

                for (int x = 0; x < 64; x++)
                {
                    _MDetail.EffectName += (char)Reader.ReadByte();
                }
                _MDetail.EffectName = _MDetail.EffectName.Trim('\0');                

                string Unknown11 = string.Empty;

                for (int x = 0; x < 64; x++)
                {
                    Unknown11 += (char)Reader.ReadByte();
                }
                Unknown11 = Unknown11.Trim('\0');

                File.Seek(196, SeekOrigin.Current);

                string Unknown12 = string.Empty;

                for (int x = 0; x < 64; x++)
                {
                    Unknown12 += (char)Reader.ReadByte();
                }
                Unknown12 = Unknown12.Trim('\0');

                string Unknown13 = string.Empty;

                for (int x = 0; x < 64; x++)
                {
                    Unknown13 += (char)Reader.ReadByte();
                }
                Unknown13 = Unknown13.Trim('\0');

                File.Seek(264, SeekOrigin.Current);

                for (int x = 0; x < 256; x++)
                {
                    _MDetail.SoundPath += (char)Reader.ReadByte();
                }
                _MDetail.SoundPath = _MDetail.SoundPath.Trim('\0');

                File.Seek(4, SeekOrigin.Current);

                string Unknown14 = string.Empty;

                for (int x = 0; x < 64; x++)
                {
                    Unknown14 += (char)Reader.ReadByte();
                }
                Unknown14 = Unknown14.Trim('\0');

                string Unknown15 = string.Empty;

                for (int x = 0; x < 64; x++)
                {
                    Unknown15 += (char)Reader.ReadByte();
                }
                Unknown15 = Unknown15.Trim('\0');

                File.Seek(12, SeekOrigin.Current);

                /*MagicLogger.WriteLine(_MDetail.SpellName + " " + _MDetail.SpellID + " " + _MDetail.Method + " " + _MDetail.ManaReq + " " + _MDetail.StatusEffect + " " + _MDetail.SuccessRate +
                    " " + _MDetail.Timer + " " + _MDetail.Level + " " + _MDetail.ManaReq + " " + _MDetail.ExpReq + " " + _MDetail.Timer + " " + _MDetail.UpgradeSkillID + " " + Unknown6 +
                    " " + Unknown7 + " " + _MDetail.Upgrade + " " + Unknown8 + " " + Unknown9 + " " + Unknown10 + " " + _MDetail.EffectName + " " + Unknown11 + " " +
                    Unknown12 + " " + Unknown13 + " " + _MDetail.SoundPath + " " + Unknown14 + " " + Unknown15);*/
            }
            Console.Write("Completed in {0}ms\n", Environment.TickCount - start);
            Monitor.Exit(Console.Out);

            File.Dispose();
            File.Close();
            Reader.Close();

            return MagicData;
        }
    }
}
